<?php
/**
 * @version		$Id: default_body.php 2011.08.11 thierry $
 * @package		admin.views.queries.tmpl
 * @subpackage	com_projid
 * @copyright	Copyright (C) 2011 Pojid All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */
// No direct access to this file
defined('_JEXEC') or die('Restricted Access');
?>
<?php foreach($this->items as $i => $item): ?>
	<tr class="row<?php echo $i % 2; ?>">
		<td>
			<?php echo $item->id_que; ?>
		</td>
		<td>
			<?php echo JHtml::_('grid.id', $i, $item->id_que); ?>
		</td>
		<td>
			<a href="<?php echo JRoute::_('index.php?option=com_projid&task=query.edit&id_que='.$item->id_que);?>">
			<?php echo $this->escape($item->desc_que); ?></a>
		</td>
		<td>
			<?PHP
				// recherchons la la configuration du serveur
				$id_requete = $item->id_que;
				$db = JFactory::getDBO();
				$query = $db->getQuery(true);
				$query->select('*');
				$query->from('#__projid_server');
				$query->leftJoin('#__projid_requete on #__projid_server.id_serv=#__projid_requete.server_id') ;
				$query->where('#__projid_requete.id_que='.$id_requete.';');
				$db->setQuery((string)$query);
				$serveur = $db->loadObject();
				$serveur->requete=$item->requete;
				// Executons maintenant la requete sur le serveur
				if ($serveur->driver=="postgres")
				{
					require_once($_SERVER['DOCUMENT_ROOT']."/components/com_projid/models/mypostgresql.php");
					$postgres=new mypostgresql($serveur);
					if (!$postgres->connection())
					{
						$result="";
						echo "Erreur de connexion";
					}
					else
					{
						if ($postgres->DoRequete())
						{$result=$postgres->_result;}
						else
						{
							$result="";
							echo "Erreur de requête";
						}
					}
				}
				
				if ($serveur->driver=="mysql")
				{
					$option = array(
					'host' 		=> $serveur->host,
					'database'	=> $serveur->base,
					'user' 		=> $serveur->user,
					'password' 	=> $serveur->password,
					'driver' 	=> $serveur->driver
					//'prefix'	=> $serveur->prefix
					);
					$query = $serveur->requete;
					$db = JDatabase::getInstance($option);
					if ($db->getErrorNum() > 0) {
						echo $db->getErrorMsg();
					}
					$db->setQuery($query); // Récupère la requête a exécuter avec l'info du serveur
					$db->query();
					$result = $db->loadRowList();
				}
				// Affichage des résultats de la requête soit les paramètres
				foreach( $result as $entry){
					if (isset($entry[1])){$entry[0]="";}else{$entry[1]="";}
					echo $entry[0].$entry[1]." - ";
				}
			?>

		</td>
		<td>
			<?php echo $item->requete; ?>
		</td>
		<td>
			<?php echo $item->desc_serv; ?>
		</td>
	
	</tr>
<?php endforeach; ?>